package support;
import java.sql.*;


public class Student {
   private String firstName;
   private String middleName;
   private String lastName;
   private String fullName;
   private String address;
   private String city;
   private String state;
   private String zipcode;
   private int citizenship;
   private int specialization; 
   
   
   public Student()
   {
	   firstName = "";
	   middleName = "";
	   lastName = "";
	   fullName = "";
	   citizenship = -1;
	   address = "";
	   state = "";
	   zipcode = "";
	   city = "";
	   specialization = -1;
   }
   
   public void firstName(String name)
   {
	   firstName = name;
   }
   
   public void middleName(String name)
   {
	   middleName = name;
   }
   
   public void lastName(String name)
   {
	   lastName = name;
   }
 
   public void fullName(String name)
   {
	   fullName = name;
   }
   
   public void citizenship(int name)
   {
	   citizenship = name;
   }
   
   public void address(String name)
   {
	   address = name;
   }
   
   public void zipcode(String name)
   {
	   zipcode = name;
   }
   
   public void state(String name)
   {
	   state = name;
   }
   
   public void city(String name)
   {
	   city = name;
   }
   
   public void specialization(int name)
   {
	   specialization = name;
   }
   
   public String getFirstName()
   {
	   return firstName;
   }
   
   public String getMiddleName()
   {
	   return middleName;
   }
   
   public String getLastName()
   {
	   return lastName;
   }

   public String getFullName()
   {
	   return fullName;
   }   
   
   public int getCitizenship()
   {
	   return citizenship;
   }
   
   public String getAddress()
   {
	   return address;
   }
   
   public String getZipcode()
   {
	   return zipcode;
   }
   
   public String getCity()
   {
	   return city;
   }
   
   public String getState()
   {
	   return state;
   }
   
   public int getSpecialization()
   {
	   return specialization;
   }
   
   // Turn country into string.
   public String getCitizenshipName()
   {
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet result = null;
		String returnedString = null;
		try 
		{
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
			conn.setAutoCommit(false);
			stm = conn.prepareStatement("SELECT * FROM countries_and_states WHERE is_country AND cs_id = ? ");
			stm.setInt(1, citizenship);
			result = stm.executeQuery();
			result.next();
			returnedString = result.getString("country_state");
			conn.commit();
			conn.setAutoCommit(true);
			result.close();
			stm.close();
			conn.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	    finally {
	        if (result != null) {
	            try {
	                result.close();
	            } 
	            catch (SQLException e) { }
	            result = null;
	        }
	        if (stm != null) {
	            try {
	                stm.close();
	            } 
	            catch (SQLException e) { }
	            stm = null;
	        }
	        if (conn != null) {
	            try {
	                conn.close();
	            } 
	            catch (SQLException e) { }
	            conn = null;
	        }
	    }
		
		return returnedString;
   }
   
   public String getSpecializationName()
   {
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet result = null;
		String returnedString = null;
		try 
		{
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
			conn.setAutoCommit(false);
			stm = conn.prepareStatement("SELECT * FROM specializations WHERE s_id = ? ");
			stm.setInt(1, specialization);
			result = stm.executeQuery();
			result.next();
			returnedString = result.getString("specialization");
			conn.commit();
			conn.setAutoCommit(true);
			result.close();
			stm.close();
			conn.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	    finally {
	        if (result != null) {
	            try {
	                result.close();
	            } 
	            catch (SQLException e) { }
	            result = null;
	        }
	        if (stm != null) {
	            try {
	                stm.close();
	            } 
	            catch (SQLException e) { }
	            stm = null;
	        }
	        if (conn != null) {
	            try {
	                conn.close();
	            } 
	            catch (SQLException e) { }
	            conn = null;
	        }
	    }
		
		return returnedString;
   }

}
